package com.css.fxfzysx.modules.ysxManagement.service.impl;

import com.css.fxfzysx.base.response.RestResponse;
import com.css.fxfzysx.modules.vaolEngineering.repository.ValEngineeringRepository;
import com.css.fxfzysx.modules.vaolEngineering.utils.MainTableUtils;
import com.css.fxfzysx.modules.ysxManagement.entity.BdFactorEntity;
import com.css.fxfzysx.modules.ysxManagement.entity.DdRatioEntity;
import com.css.fxfzysx.modules.ysxManagement.repository.BdFactorRepository;
import com.css.fxfzysx.modules.ysxManagement.service.BdFactorService;
import com.css.fxfzysx.util.PlatformDateUtils;
import com.css.fxfzysx.util.PlatformObjectUtils;
import com.css.fxfzysx.util.PlatformSessionUtils;
import com.css.fxfzysx.util.UUIDGenerator;
import com.css.fxfzysx.web.PlatformSessionContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class BdFactorServiceImpl implements BdFactorService {

    @Autowired
    private BdFactorRepository bdFactorRepository;

    @Autowired
    private ValEngineeringRepository valEngineeringRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResponse saveOrUpdateBdFactor(List<BdFactorEntity> bdFactorEntityList) {
        try {
            if (PlatformObjectUtils.isNotEmpty(bdFactorEntityList)) {
                bdFactorEntityList.stream().forEach(it -> {
                    if (PlatformObjectUtils.isEmpty(it.getCbdfId())) {
                        it.setCbdfId(UUIDGenerator.getUUID());
                        it.setCreateUser(PlatformSessionContext.getUserID());
                        it.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
                        it.setDelFlag("0");
                        it.setDefaultData("1");
                        bdFactorRepository.saveBdFactor(it);
                    } else {
                        it.setUpdateUser(PlatformSessionUtils.getUserId());
                        it.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
                        List<BdFactorEntity> list = bdFactorRepository.getBdById(it.getCbdfId());
                        list.forEach(bd->{
                            if (!bd.getCoefficientValue().equals(it.getCoefficientValue())){
                                valEngineeringRepository.updateCountStatusByModelIds1Id(it.getCbdfId());
                            }
                        });
                        bdFactorRepository.updateBdFactor(it);
                    }
                });
            }
            return RestResponse.succeed("成功!");
        } catch (Exception e) {
            String errorMessage = "失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }

    @Override
    public RestResponse getAllBdFactor() {
        try {
            List<BdFactorEntity> bdFactorList = bdFactorRepository.getAllBdFactor();
            Map<Integer, List<BdFactorEntity>> collect = bdFactorList.stream().collect(Collectors.groupingBy(BdFactorEntity::getSerialNumber));
            Map<Integer, BdFactorEntity> result = new HashMap<>();
            collect.forEach((k, v) -> {
                BdFactorEntity bd = new BdFactorEntity();
                bd.setSerialNumber(k);
                bd.setSerialData(v);
                bd.setDamageFactor(v.get(0).getDamageFactor());
                result.put(k, bd);
            });
            return RestResponse.succeed(result);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }

    @Override
    public RestResponse deleteBdFactorById(String bdFactorIds) {
        try {
            boolean result = true;
            for (String bdFactorId : bdFactorIds.split(",")) {
                result = bdFactorRepository.deleteBdFactorById(bdFactorId);
            }
            if (result) {
                return RestResponse.succeed("删除成功!");
            } else {
                return RestResponse.fail("删除失败!");
            }
        } catch (Exception e) {
            String errorMessage = "删除失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }
}
